package com.jinlejia.erp.ui.house;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.MyViewPagerAdapter;
import com.jinlejia.erp.databinding.FragmentHouseTabBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/28 0028.
 */

public class HouseTabFragment extends Fragment {

    // TabLayout中的tab标题  ,
    private String[] mTitles = {"维护信息", "交易属性","分间信息"};

    private FragmentHouseTabBinding binding;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_house_tab, null);
        binding = DataBindingUtil.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        List<Fragment> mFragments= new ArrayList<>();
        mFragments.add(0, new MaintainFragment());
        mFragments.add(1, new PropertyFragment());
        mFragments.add(2,new InformationFragment());
        // 初始化ViewPager的适配器，并设置给它
        MyViewPagerAdapter mViewPagerAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager(), mTitles, mFragments);
        binding.viewpager.setAdapter(mViewPagerAdapter);
        binding.viewpager.setOffscreenPageLimit(mTitles.length);

        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        binding.tablayout.setTabsFromPagerAdapter(mViewPagerAdapter);
        binding.tablayout.post(() -> {
            // 将TabLayout和ViewPager进行关联，让两者联动起来
            binding.tablayout.setupWithViewPager(binding.viewpager);
        });

    }

}
